package com.twu.biblioteca.services;

import com.twu.biblioteca.domain.*;
import com.twu.biblioteca.repositories.IUserRepository;

public class LibraryService {
    private ILibraryIOService libraryIOService;
    private Library library;
    private IUserRepository userRepository;
    private User currentUser;

    public LibraryService(Library library, ILibraryIOService libraryIOService, IUserRepository userRepository) {
        this.library = library;
        this.libraryIOService = libraryIOService;
        this.userRepository = userRepository;
    }

    public void run() {
        showWelcomeMessage();

        while (true) {
            showMenu();

            MenuOption option = libraryIOService.getMenuInput();

            switch (option) {
                case LIST_BOOKS:
                    showBooks();
                    break;

                case RESERVE_BOOK:
                    if (userIsLoggedIn())
                        reserveBook();
                    else
                        showMessageUserNotLoggedIn();
                    break;

                case CHECK_MEMBERSHIP:
                    if (userIsLoggedIn())
                        showMembershipDetails();
                    else
                        showMessageUserNotLoggedIn();
                    break;

                case LIST_MOVIES:
                    showMovies();
                    break;

                case LOGIN:
                      loginUser();
                    break;
                case EXIT:
                    return;

                case INVALID:
                    libraryIOService.output("Select a valid option!!");
                    break;
            }
        }
    }

    private void loginUser() {
        libraryIOService.output("Username:");
        String userName = libraryIOService.getTextInput();
        libraryIOService.output("Password:");
        String password = libraryIOService.getTextInput();

        this.currentUser = userRepository.getUser(userName, password);

        if (!userIsLoggedIn())
            libraryIOService.output("Unknown username or password.");
    }

    private void showMessageUserNotLoggedIn() {
        libraryIOService.output("Please, log in first.");
    }

    private boolean userIsLoggedIn() {
        return this.currentUser != null;
    }

    private void showMovies() {

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("Movie | Year | Director | Rating");
        stringBuilder.append("\n");

        for (Movie movie : library.getMovies()) {
            stringBuilder.append(movie.toString());
            stringBuilder.append("\n");
        }

        libraryIOService.output(stringBuilder.toString());
    }

    private void reserveBook() {
        libraryIOService.output("Which book?");
        String bookTitle = libraryIOService.getTextInput();

        try {
            library.reserveBook(bookTitle);
            libraryIOService.output("Thank You! Enjoy the book.");
        } catch (BookNotAvailableException e) {
            libraryIOService.output("Sorry, we don't have that book yet.");
        }
    }

    private void showMembershipDetails() {
        libraryIOService.output(currentUser.toString());
    }

    private void showBooks() {
        StringBuilder stringBuffer = new StringBuilder();

        for (Book book : library.getBooks())
            stringBuffer.append(book.toString()).append("\n");

        libraryIOService.output(stringBuffer.toString());
    }

    private void showWelcomeMessage() {
        libraryIOService.output("Welcome!");
    }

    private void showMenu() {
        String menu = "1 - Check our books!\n" +
                "2 - Reserve a book\n" +
                "3 - Membership details\n" +
                "4 - Check our movies!\n" +
                "5 - Log In\n" +
                "6 - Exit";

        this.libraryIOService.output(menu);
    }
}
